<?php
/**
 * 系统日志处理类
 *
 * @author Mo yi <root@imoi.cn>
 * @link   http://www.imoi.cn
 * @copyright Copyright &copy; 2010-2012 DuoLam Software LLC
 * @license http://www.imoi.cn/license/
 * @version $Id: dpLoging.php 2799 2012-04-03 16:09:13Z Mo.yi $
 * @package system
 * @since 1.0
 */
class dpLoging extends dpComponent
{
	/**
	 * 写入日志
	 * @param string $content 日志内容
	 */
	public static function writeLog($content)
	{
		$request = new dpRequest;
		$rs   = @fopen(self::getFileName(), 'a+');
		$text = '时间：'. date('Y-m-d H:i:s') . ', 信息：'. $content . ', IP：' . $request->getUserIp();
		$text.= ', 来源页面：'.$request->getUrlSource() . ', 浏览器：'.$request->getUserBrowser();
		$text.= ', 操作系统：'.$request->getUserOs();
		$text.= ', 错误页面：'.$request->getHostInfo().$_SERVER['REQUEST_URI'] . "\r\n";
		@fwrite($rs, $text);
		@fclose($rs);
		unset($request);
	}

	/**
	 * 获取日志文件名
	 */
	public static function getFileName()
	{
		$path = APP_PATH . DS . 'runtime' . DS . 'log';

		if (!is_dir($path)) mkdir($path, 0755, true);

		$rs = opendir($path);
		$i  = 1;

		while ($file = readdir($rs)) {
			if ($file == '.' or $file == '..') continue;
			$i++;
		}

		closedir($rs);

		if ($i == 1) {
			$name = 'application1.log';
			$file = $path . DS . $name;

			if (is_file($file)) {
				if(filesize($file) > 512000)
					$file = $path . DS . 'application2.log';
			}
		} else {
			$pre = $i - 1;
			$file = $path . DS . 'application'.$pre.'.log';

			if (filesize($file) > 512000)
				$file = $path . DS . 'application'.$i.'.log';
		}
		
		return $file;
	}
}
?>